import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'


const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		phone: storage.get(constant.phone),
		email: storage.get(constant.email),
		sex: storage.get(constant.sex),
		birthday: storage.get(constant.birthday),
		deptId: storage.get(constant.deptId),
		type: storage.get(constant.type),
		avatar: storage.get(constant.avatar),
		roles: storage.get(constant.roles),
		permissions: storage.get(constant.permissions),
		introduction: storage.get(constant.introduction),
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_SEX: (state, sex) => {
			state.sex = sex
			storage.set(constant.sex, sex)
		},
		SET_PHONE: (state, phone) => {
			state.phone = phone
			storage.set(constant.phone, phone)
		},
		SET_EMAIL: (state, email) => {
			state.email = email
			storage.set(constant.email, email)
		},
		SET_BIRTHDAY: (state, birthday) => {
			state.birthday = birthday
			storage.set(constant.birthday, birthday)
		},
		SET_DEPT_ID: (state, deptId) => {
			state.deptId = deptId
			storage.set(constant.deptId, deptId)
		},
		SET_TYPE: (state, type) => {
			state.type = type
			storage.set(constant.type, type)
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
			storage.set(constant.roles, roles)
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		},
		SET_INTRODUCTION: (state, introduction) => {
			state.introduction = introduction
			storage.set(constant.introduction, introduction)
		},
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const phone = userInfo.phone;
			const password = userInfo.password;
			return new Promise((resolve, reject) => {
				login({
					phone,
					password,
				}).then(response => {
					setToken(response.data)
					commit('SET_TOKEN', response.data)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					const user = res.data
					const avatar = !user.avatar ? require("@/static/avatar.png") : user.avatar
					const username = user.nickName;
					if (res.roles && res.roles.length > 0) {
						commit('SET_ROLES', res.roles)
						commit('SET_PERMISSIONS', res.permissions)
					} else {
						commit('SET_ROLES', ['ROLE_DEFAULT'])
					}
					commit('SET_NAME', username)
					commit('SET_AVATAR', avatar)
					commit('SET_SEX', user.sex)
					commit('SET_PHONE', user.phone)
					commit('SET_EMAIL', user.email)
					commit('SET_BIRTHDAY', user.birthday)
					commit('SET_DEPT_ID', user.deptId)
					commit('SET_TYPE', user.type || '0')
					commit('SET_INTRODUCTION', user.introduction)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout().then(() => {
					resolve(true)
				}).catch(error => {
					resolve(false)
				}).finally(() => {
					commit('SET_TOKEN', '')
					commit('SET_ROLES', [])
					commit('SET_NAME', '')
					commit('SET_AVATAR', '')
					commit('SET_PHONE', '')
					commit('SET_EMAIL', '')
					commit('SET_BIRTHDAY', '')
					commit('SET_DEPT_ID', '')
					commit('SET_SEX', '')
					commit('SET_TYPE', '')
					commit('SET_PERMISSIONS', [])
					commit('SET_INTRODUCTION', '')
					removeToken()
					storage.clean()
				})
			})
		}
	}
}

export default user